package com.example.gramediamajalahmobile.favorite;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.gramediamajalahmobile.R;
import com.example.gramediamajalahmobile.sqlite.DBHelperFavorite;

public class Favorite extends Activity {
	DBHelperFavorite db;

	EditText editName, editPhone;
	Button btnSave, btnDelete;

	int id;
	ImageButton gambar;
	TextView title, tanggal, view, source;
	WebView body;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite);
		db = new DBHelperFavorite(this);
		db.open();

		Bundle b = getIntent().getExtras();
		id = (int) b.getLong(FavoriteList.KEY_ID);

		
		title = (TextView) findViewById(R.id.titlef);
		tanggal = (TextView) findViewById(R.id.tanggalf);
		view = (TextView) findViewById(R.id.viewf);
		gambar = (ImageButton) findViewById(R.id.gambarf);
		
		body = (WebView) findViewById(R.id.bodyf);
		
		// TODO Auto-generated method stub
		
				Cursor cursor = db.getSingleArticle(id);
				final String title_f = cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_TITLE));
				final String gambar_f = cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_GAMBAR));
				final String tanggal_f = cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_TANGGAL));
				final String view_f= cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_VIEW));
				final String source_f =cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_SOURCE));
				final String body_f = cursor.getString(cursor.getColumnIndexOrThrow(DBHelperFavorite.COL_BODY));
				
				long timestamp = Long.parseLong(tanggal_f) * 1000;
				SimpleDateFormat sdf = new SimpleDateFormat("dd MMMM yyyy");
				String date = sdf.format(timestamp);

				
				title.setText(title_f);
				view.setText("view : " + view_f);
				tanggal.setText(date);
				source.setText(source_f);
				body.loadData(body_f, "text/html", "UTF-8");

				loadImage(gambar_f);
				gambar.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						switch (v.getId()) {
						case R.id.gambar:
							setContentView(R.layout.imagezoom);

							// load view
							WebView zoom = (WebView) findViewById(R.id.zoom);
							// (*) this line make uses of the Zoom control
							zoom.getSettings().setBuiltInZoomControls(true);
							zoom.loadUrl(gambar_f);
						}
					}
				});

	}

	// private void tampilData() {
	//
	// }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
//
//	@Override
//	public void onClick(View v) {
//		// TODO Auto-generated method stub
//		switch (v.getId()) {
//		case R.id.editBtnSave:
//
//			Contact contact = new Contact();
//			contact.setId(id);
//			contact.setName(editName.getText().toString());
//			contact.setTelp(editPhone.getText().toString());
//
//			db.updateContact(contact);
//			startActivity(new Intent(EditActivity.this, MainActivity.class));
//			finish();
//			break;
//		case R.id.editBtnDelete:
//
//			db.deleteContact(id);
//			startActivity(new Intent(EditActivity.this, MainActivity.class));
//			finish();
//			break;
//		}
//
//	}

	Bitmap bitmap;

	void loadImage(String image_location) {

		URL imageURL = null;

		try {
			imageURL = new URL(image_location);
		}

		catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection connection = (HttpURLConnection) imageURL.openConnection();
			connection.setDoInput(true);
			connection.connect();
			InputStream inputStream = connection.getInputStream();

			bitmap = BitmapFactory.decodeStream(inputStream);// Convert to
																// bitmap
			gambar.setImageBitmap(bitmap);

			// imaged.setBitmap(bitmap);
		} catch (IOException e) {

			e.printStackTrace();
		}
	}

}